package com.malware.detection;




import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.malware.detection.SystemBehaviour.LocalBinder;

import android.app.Activity;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;

import android.os.Bundle;
import android.os.IBinder;


import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import android.widget.TextView;
public class DetectActivity extends ListActivity   {


	public static final String TAG = "PoornimaMalware";
	SystemBehaviour myService;
	boolean isBound = false;
	boolean callMade = false;

	List<String> listDuring=new ArrayList<String>();
	ArrayAdapter<String> adapterDuring;
	BroadcastReceiver serviceReceiver;
	BroadcastReceiver afterserviceReceiver;

	static final String[] selectOptions = new String[] { "During the call", "After the call"};
	Intent duringCallIntent;
	Intent afterCallIntent; 
	Bundle afterResBundle;
	Bundle duringResBundle;
	Context context;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		Log.d(TAG, "DetectActivity: Inside OnCreate!!!");
		setListAdapter(new ArrayAdapter<String>(this, R.layout.action_list,selectOptions));
		ListView listView = getListView();
		listView.setTextFilterEnabled(true);

		
		


		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long arg3) {

				String itemname = selectOptions[position];

			//	if(callMade){

					Toast.makeText(getApplicationContext(),
							((TextView) view).getText(), Toast.LENGTH_SHORT).show();

					if(itemname.equals("During the call")){
						duringCallIntent = new Intent(context,DuringCall.class);
						duringCallIntent.putExtras(duringResBundle);
						Log.d(TAG, "Detect Activity!!! during bundle memory" + duringResBundle.getLong("memoryDuringCall"));
						Log.d(TAG, "Detect Activity!!! during bundle apps" + duringResBundle.getStringArrayList("duringList"));
						startActivityForResult(duringCallIntent, 0);
					}
					else if(itemname.equals("After the call")){
						afterCallIntent = new Intent(context,AfterCall.class); 
						afterCallIntent.putExtras(afterResBundle);
						Log.d(TAG, "Detect Activity!!! after bundle memory" + afterResBundle.getLong("memoryAfterCall"));
						Log.d(TAG, "Detect Activity!!! after bundle apps" + afterResBundle.getStringArrayList("afterList"));
						startActivityForResult(afterCallIntent, 1);
					}
				//}else
				//	Toast.makeText(getApplicationContext(),
				//			"No call is made", Toast.LENGTH_SHORT).show();
			}
		});


 		final IntentFilter theFilter = new IntentFilter();
		theFilter.addAction("malwareDetection.intent");
		this.serviceReceiver=new BroadcastReceiver() {
 
			@Override
			public void onReceive(Context context, Intent intent) {
				callMade = true;
				//Log.d(TAG, "Detect Activity Inside On Receive!!!");

				duringResBundle = new Bundle();
				//Log.d(TAG, "Detect Activity!!! memory during call" + myService.get_mem_duringcall());
				//Log.d(TAG, "Detect Activity!!! app list during call" + myService.get_duringCall_result());

				duringResBundle.putLong("memoryDuringCall", myService.get_mem_duringcall());
				duringResBundle.putStringArrayList("duringList", (ArrayList<String>) myService.get_duringCall_result());
				Log.d(TAG, "Detect Activity!!! during call" + duringResBundle.keySet());
				//Log.d(TAG, "Detect Activity!!! during call" + duringCallIntent.getStringExtra("duringCallResult"));
			}
		};
		this.registerReceiver(serviceReceiver,theFilter);
		
		
 		final IntentFilter theAfterFilter = new IntentFilter();
 		theAfterFilter.addAction("malwareDetection.after.intent");
		this.afterserviceReceiver=new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				callMade = true;
				//Log.d(TAG, "Detect Activity after call: Inside On Receive!!!");
 
				afterResBundle = new Bundle();
				//Log.d(TAG, "Detect Activity!!! memory after call" + myService.get_mem_aftercall());
				//Log.d(TAG, "Detect Activity!!! app list after call" + myService.get_afterCall_result());

				afterResBundle.putLong("memoryAfterCall", myService.get_mem_aftercall());
				afterResBundle.putStringArrayList("afterList", (ArrayList<String>) myService.get_afterCall_result());
				Log.d(TAG, "Detect Activity!!! after call" + afterResBundle.keySet());
				//Log.d(TAG, "Detect Activity!!! after call" + afterCallIntent.getStringExtra("duringCallResult"));

			}
		};
		this.registerReceiver(afterserviceReceiver,theAfterFilter);
		//Binding to  SystemBehavior
		
		
		Intent serviceIntent=new Intent(this,SystemBehaviour.class);
		ComponentName myService = startService(serviceIntent);
		bindService(serviceIntent, myConnection, Context.BIND_AUTO_CREATE);
	}

	//This is for binding to the service
	private ServiceConnection myConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className,IBinder service) {
			LocalBinder binder = (LocalBinder) service;
			myService = binder.getService();
			isBound = true;
		}

		public void onServiceDisconnected(ComponentName arg0) {
			isBound = false;
		}
	};

	@Override
	protected void onDestroy()
	{	
		super.onDestroy();
		if (afterserviceReceiver != null) {
			unregisterReceiver(afterserviceReceiver);
		}
		if (serviceReceiver != null) {
			unregisterReceiver(serviceReceiver);
		}
		
		if(myConnection!=null){
			unbindService(myConnection);
			myConnection = null;
		}
	}


}
